package com.demomaster.quick_reader.model.novel;

import android.content.Context;
import android.graphics.Color;

import com.demomaster.quick_reader.widget.page.PageMode;
import com.demomaster.quick_reader.util.ScreenUtils;
import com.demomaster.quick_reader.util.SharedPreUtils;
import com.demomaster.quick_reader.widget.page.PageStyle3;

/**
 * Created by newbiechen on 17-5-17.
 * 阅读器的配置管理
 */

public class ReadSettingManager implements ReadSettingManagerInterface{
    /*************实在想不出什么好记的命名方式。。******************/
    public static final int READ_BG_DEFAULT = 0;
    public static final int READ_BG_1 = 1;
    public static final int READ_BG_2 = 2;
    public static final int READ_BG_3 = 3;
    public static final int READ_BG_4 = 4;
    public static final int NIGHT_MODE = 5;

    //public static final String SHARED_READ_BG = "shared_read_bg";
    public static final String SHARED_READ_BG_Color = "shared_read_bg_Color";
    public static final String SHARED_READ_BG_Font_Color = "shared_read_bg_Font_Color";
    public static final String SHARED_READ_BG_POSITION = "SHARED_READ_BG_POSITION";
    public static final String SHARED_READ_BRIGHTNESS = "shared_read_brightness";
    public static final String SHARED_READ_IS_BRIGHTNESS_AUTO = "shared_read_is_brightness_auto";
    public static final String SHARED_READ_TEXT_SIZE = "shared_read_text_size";
    public static final String SHARED_READ_IS_TEXT_DEFAULT = "shared_read_text_default";
    public static final String SHARED_READ_PAGE_MODE = "shared_read_mode";
    public static final String SHARED_READ_NIGHT_MODE = "shared_night_mode";
    public static final String SHARED_READ_VOLUME_TURN_PAGE = "shared_read_volume_turn_page";
    public static final String SHARED_READ_FULL_SCREEN = "shared_read_full_screen";
    public static final String SHARED_READ_CONVERT_TYPE = "shared_read_convert_type";

    int[] backgroundColors = {
            com.demomaster.quick_reader.R.color.nb_read_bg_1,
            com.demomaster.quick_reader.R.color.nb_read_bg_2,
            com.demomaster.quick_reader.R.color.nb_read_bg_3,
            com.demomaster.quick_reader.R.color.nb_read_bg_4,
            com.demomaster.quick_reader.R.color.nb_read_bg_5
    };
    int[] backgroundColorResIds =  {
            com.demomaster.quick_reader.R.color.nb_read_bg_1,
            com.demomaster.quick_reader.R.color.nb_read_bg_2,
            com.demomaster.quick_reader.R.color.nb_read_bg_3,
            com.demomaster.quick_reader.R.color.nb_read_bg_4,
            com.demomaster.quick_reader.R.color.nb_read_bg_5
    };

    /**
     * 设置背景可选色
     * @param backgroundColors
     */
    public void setBackgroundColors(int[] backgroundColors) {
        this.backgroundColors = backgroundColors;
    }

    public int[] getBackgroundColors() {
        return backgroundColors;
    }

    private static volatile ReadSettingManager sInstance;

    private SharedPreUtils sharedPreUtils;

    public static ReadSettingManager getInstance(Context context) {
        if (sInstance == null) {
            synchronized (ReadSettingManager.class) {
                if (sInstance == null) {
                    sInstance = new ReadSettingManager(context);
                }
            }
        }
        return sInstance;
    }

    private ReadSettingManager(Context context) {
        sharedPreUtils = SharedPreUtils.getInstance(context);
        for(int i = 0; i < backgroundColorResIds.length; i++){
            backgroundColors[i]=context.getResources().getColor(backgroundColorResIds[i]);
        }
    }

//    public void setPageStyle(PageStyle pageStyle) {
//        sharedPreUtils.putInt(SHARED_READ_BG, pageStyle.ordinal());
//    }

    public void setPageStyle(PageStyle3 pageStyle) {
       // sharedPreUtils.putInt(SHARED_READ_BG, pageStyle.ordinal());
        if(!pageStyle.isNight()) {
            sharedPreUtils.putInt(SHARED_READ_BG_Color, pageStyle.getBgColor());
            sharedPreUtils.putInt(SHARED_READ_BG_Font_Color, pageStyle.getFontColor());
            sharedPreUtils.putInt(SHARED_READ_BG_POSITION, pageStyle.getOrdinal());
        }
    }
    public void setBrightness(int progress) {
        sharedPreUtils.putInt(SHARED_READ_BRIGHTNESS, progress);
    }

    public void setAutoBrightness(boolean isAuto) {
        sharedPreUtils.putBoolean(SHARED_READ_IS_BRIGHTNESS_AUTO, isAuto);
    }

    public void setDefaultTextSize(boolean isDefault) {
        sharedPreUtils.putBoolean(SHARED_READ_IS_TEXT_DEFAULT, isDefault);
    }

    public void setTextSize(int textSize) {
        sharedPreUtils.putInt(SHARED_READ_TEXT_SIZE, textSize);
    }

    public void setPageMode(PageMode mode) {
        sharedPreUtils.putInt(SHARED_READ_PAGE_MODE, mode.ordinal());
    }

    public void setNightMode(boolean isNight) {
        sharedPreUtils.putBoolean(SHARED_READ_NIGHT_MODE, isNight);
    }

    public int getBrightness() {
        return sharedPreUtils.getInt(SHARED_READ_BRIGHTNESS, 40);
    }

    public boolean isBrightnessAuto() {
        return sharedPreUtils.getBoolean(SHARED_READ_IS_BRIGHTNESS_AUTO, false);
    }

    public int getTextSize(Context context) {
        return sharedPreUtils.getInt(SHARED_READ_TEXT_SIZE, ScreenUtils.spToPx(context,28));
    }

    public boolean isDefaultTextSize() {
        return sharedPreUtils.getBoolean(SHARED_READ_IS_TEXT_DEFAULT, false);
    }

    public PageMode getPageMode() {
        int mode = sharedPreUtils.getInt(SHARED_READ_PAGE_MODE, PageMode.SIMULATION.ordinal());
        return PageMode.values()[mode];
    }
//    public PageStyle getPageStyle() {
//        int style = sharedPreUtils.getInt(SHARED_READ_BG, PageStyle.BG_0.ordinal());
//        return PageStyle.values()[style];
//    }
    public PageStyle3 getPageStyle3() {
        if(isNightMode()){
            return PageStyle3.getNightStyle();
        }
        int bgColor = sharedPreUtils.getInt(SHARED_READ_BG_Color, backgroundDefaultColor);
        int fontColor = sharedPreUtils.getInt(SHARED_READ_BG_Font_Color, fontDefaultColor);

        int ordinal = sharedPreUtils.getInt(SHARED_READ_BG_POSITION, 1);
        PageStyle3 pageStyle3 = new PageStyle3();
        pageStyle3.setBgColor(bgColor);
        pageStyle3.setOrdinal(ordinal);
        pageStyle3.setFontColor(fontColor);
        return pageStyle3;
    }

    public boolean isNightMode() {
        return sharedPreUtils.getBoolean(SHARED_READ_NIGHT_MODE, false);
    }

    public void setVolumeTurnPage(boolean isTurn) {
        sharedPreUtils.putBoolean(SHARED_READ_VOLUME_TURN_PAGE, isTurn);
    }

    public boolean isVolumeTurnPage() {
        return sharedPreUtils.getBoolean(SHARED_READ_VOLUME_TURN_PAGE, false);
    }

    public void setFullScreen(boolean isFullScreen) {
        sharedPreUtils.putBoolean(SHARED_READ_FULL_SCREEN, isFullScreen);
    }

    public boolean isFullScreen() {
        return sharedPreUtils.getBoolean(SHARED_READ_FULL_SCREEN, false);
    }

    public void setConvertType(int convertType) {
        sharedPreUtils.putInt(SHARED_READ_CONVERT_TYPE, convertType);
    }

    public int getConvertType() {
        return sharedPreUtils.getInt(SHARED_READ_CONVERT_TYPE, 0);
    }

    int backgroundDefaultColor = 0xffffffff;
    int fontDefaultColor= 0xff000000;
    public void setDefaultBgColorAndFrontColor(int bgColor, int fontColor) {
        this.fontDefaultColor = fontColor;
        this.backgroundDefaultColor = bgColor;
    }
}
